﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using Pibby.Core.Persistence;

namespace Pibby.Core.Tests
{
	[TestClass]
	public class IdentifiableTests
	{
		/// <summary>
		/// Needed because Identifiable is an abstract class
		/// </summary>
		class TestIdentifiableObject : Identifiable
		{
			public TestIdentifiableObject()
				: base()
			{
			}

			public TestIdentifiableObject(string id)
				: base(id)
			{
			}
		}

		[TestMethod]
		public void CanCreateWithKnownId()
		{
			const string knownId = "12345";
			TestIdentifiableObject obj = new TestIdentifiableObject(knownId);

			Assert.AreEqual(knownId, obj.Id);

			Assert.AreEqual(knownId, obj.ToString());
		}

		[TestMethod]
		public void AutoAssignIdIfNotSpecifiedAtConstruction()
		{
			TestIdentifiableObject obj = new TestIdentifiableObject();

			//Assert.IsNotEmpty(obj.Id);

			Assert.AreEqual(obj.Id, obj.ToString());
		}

		[TestMethod]
		public void CanSetIdOnceOnly()
		{
			TestIdentifiableObject obj = new TestIdentifiableObject();

			const string knownId = "1234567890";

			obj.Id = knownId;

			Assert.AreEqual(knownId, obj.Id);
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentException))]
		public void CannotChangeIdOnceSet()
		{
			TestIdentifiableObject obj = new TestIdentifiableObject();

			const string knownId = "1234567890";

			obj.Id = knownId;
			obj.Id = "9876543210";

			Assert.Fail("Expecting exception for re-assignment of id");
		}

		[TestMethod]
		public void PesistableGuidIsFormattedForStorage()
		{
			TestIdentifiableObject obj = new TestIdentifiableObject();

			const string knownId = "1234567890";

			obj.Id = knownId;

			Assert.AreEqual("{1234567890}", obj.PersistableGuid);
		}

		[TestMethod]
		public void FromStorageIdIsSetCorrectlyFromPesistableGuid()
		{
			TestIdentifiableObject obj1 = new TestIdentifiableObject();

			const string knownId = "{1234567890}";

			obj1.PersistableGuid = knownId;

			Assert.AreEqual("1234567890", obj1.Id);

			TestIdentifiableObject obj2 = new TestIdentifiableObject();

			obj2.PersistableGuid = obj1.Id;

			Assert.AreEqual("1234567890", obj2.Id);
		}

	}
}
